package com.project.biz.impl;

import com.alibaba.fastjson.JSONObject;
import com.project.common.enums.CapitalFlowEnum;
import com.project.common.enums.LoginEnum;
import com.project.pojo.dto.CapitalFlowOldOneStatisticsDTO;
import com.project.pojo.dto.UserTolProfitDTO;
import com.project.redis.RedisClient2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @Author: zhuchi
 * @CreateDate: 2020/9/11 18:45
 * @Version: 1.0
 */
@Service
public class RedisBiz2Impl implements com.project.biz.RedisBiz2 {

    /**
     * Redis服务
     */
    @Autowired
    private RedisClient2 redisClient2;

    /**
     * 缓存资金流水（旧1）表统计数据
     * @param capitalFlowOldOneStatisticsDTO
     * @return
     */
    public boolean setCapitalFlowOne(CapitalFlowOldOneStatisticsDTO capitalFlowOldOneStatisticsDTO){
        String jsonStr = JSONObject.toJSONString(capitalFlowOldOneStatisticsDTO);
        boolean result =  this.redisClient2.hset(CapitalFlowEnum.CAPITAL_FLOW_OLD.Value(), CapitalFlowEnum.CAPITAL_FLOW_OLD_ONE.Value(), jsonStr);
        return result;
    }

    /**
     * 获取缓存资金流水（旧1）表统计数据
     * @return
     */
    public CapitalFlowOldOneStatisticsDTO getCapitalFlowOne(){
        if (checkCapitalFlowOne()){
            String jsonStr = this.redisClient2.hget(CapitalFlowEnum.CAPITAL_FLOW_OLD.Value(), CapitalFlowEnum.CAPITAL_FLOW_OLD_ONE.Value());
            CapitalFlowOldOneStatisticsDTO capitalFlowOldOneStatisticsDTO = JSONObject.toJavaObject(JSONObject.parseObject(jsonStr), CapitalFlowOldOneStatisticsDTO.class);
            return capitalFlowOldOneStatisticsDTO;
        }
        return null;
    }

    /**
     * 验证缓存资金流水（旧1）表统计数据是否缓存过
     * @return
     */
    public boolean checkCapitalFlowOne(){
        return this.redisClient2.hHasKey(CapitalFlowEnum.CAPITAL_FLOW_OLD.Value(), CapitalFlowEnum.CAPITAL_FLOW_OLD_ONE.Value());
    }

    /**
     * 清空资金流水（旧1）表统计缓存数据
     */
    public void delCapitalFlowOne(){
        if (this.checkCapitalFlowOne()){
            this.redisClient2.del(CapitalFlowEnum.CAPITAL_FLOW_OLD.Value());
        }
    }

    /**
     * 缓存用户资金流水（旧1）表统中统计数据
     * @param userId
     * @param userTolProfitDTO
     * @return
     */
    public boolean setUserTolProfit(Long userId, UserTolProfitDTO userTolProfitDTO){
        String jsonStr = JSONObject.toJSONString(userTolProfitDTO);
        boolean result =  this.redisClient2.set(CapitalFlowEnum.OLD_USER_TOLPROFIT.Value() + userId, jsonStr);
        return result;
    }

    /**
     * 获取缓存中用户资金流水（旧1）表统中统计数据
     * @param userId
     * @return
     */
    public UserTolProfitDTO getUserTolProfit(Long userId){
        if (checkUserTolProfit(userId)){
            String jsonStr = this.redisClient2.get(CapitalFlowEnum.OLD_USER_TOLPROFIT.Value() + userId);
            UserTolProfitDTO userTolProfitDTO = JSONObject.toJavaObject(JSONObject.parseObject(jsonStr), UserTolProfitDTO.class);
            return userTolProfitDTO;
        }
        return null;
    }

    /**
     * 验证缓存中用户资金流水（旧1）表统计数据是否缓存过
     * @param userId
     * @return
     */
    public boolean checkUserTolProfit(Long userId){
        return this.redisClient2.hasKey(CapitalFlowEnum.OLD_USER_TOLPROFIT.Value() + userId);
    }

    /**
     * 后台统计信息增加缓存
     * @param pcStatisticsDTOJson
     * @return
     */
    public boolean setPcStatistics(String pcStatisticsDTOJson){
        boolean result =  this.redisClient2.set(LoginEnum.PCSTATISTICS_KEY.Value(), pcStatisticsDTOJson);
        redisClient2.expire(LoginEnum.PCSTATISTICS_KEY.Value(), 60*5);//缓存五分钟
        return result;
    }

    /**
     * 获取缓存后台统计信息
     * @return
     */
    public String getPcStatistics(){
        String result =  this.redisClient2.get(LoginEnum.PCSTATISTICS_KEY.Value());
        return result;
    }

    /**
     * 验证后台统计信息是否加过缓存
     * @return
     */
    public boolean checkPcStatistics(){
        return this.redisClient2.hasKey(LoginEnum.PCSTATISTICS_KEY.Value());
    }

    /**
     * 清空后台统计信息缓存
     */
    public void delPcStatistics(){
        if (this.checkPcStatistics()){
            this.redisClient2.del(LoginEnum.PCSTATISTICS_KEY.Value());
        }
    }

    /**
     * H5统计信息增加缓存
     * @param h5StatisticsDTOJson
     * @return
     */
    public boolean setH5Statistics(String h5StatisticsDTOJson){
        boolean result =  this.redisClient2.set(LoginEnum.H5STATISTICS_KEY.Value(), h5StatisticsDTOJson);
        redisClient2.expire(LoginEnum.H5STATISTICS_KEY.Value(), 60*5);//缓存五分钟
        return result;
    }

    /**
     * 获取缓存H5统计信息
     * @return
     */
    public String getH5Statistics(){
        String result =  this.redisClient2.get(LoginEnum.H5STATISTICS_KEY.Value());
        return result;
    }

    /**
     * 验证H5统计信息是否加过缓存
     * @return
     */
    public boolean checkH5Statistics(){
        return this.redisClient2.hasKey(LoginEnum.H5STATISTICS_KEY.Value());
    }

    /**
     * 清空H5统计信息缓存
     */
    public void delH5Statistics(){
        if (this.checkH5Statistics()){
            this.redisClient2.del(LoginEnum.H5STATISTICS_KEY.Value());
        }
    }

    /**
     * 设置系统配置信息缓存
     * @param systemConfJson
     * @return
     */
    public boolean setSystemConf(String systemConfJson){
        boolean result =  this.redisClient2.set(LoginEnum.SET_SYSTEMCONF.Value(), systemConfJson);
        return result;
    }

    /**
     * 获取系统配置信息缓存
     * @return
     */
    public String getSystemConf(){
        if (this.checkSystemConf()){
            return this.redisClient2.get(LoginEnum.SET_SYSTEMCONF.Value());
        }
        return null;
    }

    /**
     * 验证系统配置信息缓存
     * @return
     */
    public boolean checkSystemConf(){
        return this.redisClient2.hasKey(LoginEnum.SET_SYSTEMCONF.Value());
    }

}
